/*
 * Purpose: The top-level jframe window
 */
package View;

import Controller.IDEManager;
import Model.Programmable;
import Util.Execute;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

/**
 *
 * @author jase
 */
public class MainWindow extends JFrame
	implements ActionListener {

	// constants
	private static final String CREATE = "new";
	private static final String OPEN = "open";
	private static final String DELETE = "delete";
	private static final String ABOUT = "about";
	private static final String H2OHELP = "H2O Help";
	private static final String EXECUTE = "run";
	private static final String H20TITLE = "H20 Project Editor (v" + 1.51 + ")";
	// UI elements
	private JPanel mainPanel;
	private JLabel statusBar;
	private EditorPanel editorPanel;
	private JMenu projectMenu, fileMenu, helpMenu;
	private JMenuItem runProject;
	private static MainWindow instance;
	private IDEManager manager;

	public static void create() {
		if (instance == null) {
			instance = new MainWindow();
		}
	}

	private MainWindow() {
		// basic setup
		setTitle("");
		addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				manager.shutdown();
				editorPanel.removeAll();
				System.out.println("shutdown!");
				System.exit(0);
			}
		});


		// setup the sub-window structure
		mainPanel = new JPanel();
		mainPanel.setPreferredSize(
			new Dimension(900, 600));
		editorPanel = new EditorPanel();
		statusBar = new JLabel("ready...");
		statusBar.setPreferredSize(
			new Dimension(100, 40));
		statusBar.setHorizontalAlignment(
			JLabel.CENTER);
		statusBar.setFont(
			new Font(Font.SERIF, Font.PLAIN, 22));
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(editorPanel.asComponent(),
			BorderLayout.CENTER);
		mainPanel.add(statusBar,
			BorderLayout.SOUTH);

		// setup the menu
		JMenuBar menuBar = new JMenuBar();
		projectMenu = new JMenu("Project");
		helpMenu = new JMenu("Help");
		fileMenu = new JMenu("File");

		projectMenu.add(new JMenuItem(OPEN));
		projectMenu.add(new JMenuItem(CREATE));
		runProject = new JMenuItem(EXECUTE);
		projectMenu.add(runProject);
		runProject.setEnabled(false);
//    projectMenu.add(new JMenuItem(DELETE));

		fileMenu.add(new JMenuItem(CREATE));
//    fileMenu.add(new JMenuItem(OPEN));
		fileMenu.add(new JMenuItem(DELETE));
		fileMenu.setEnabled(false); // initially disabled

		helpMenu.add(new JMenuItem(ABOUT));
		helpMenu.add(new JMenuItem(H2OHELP));

		menuBar.add(projectMenu);
		menuBar.add(fileMenu);
		menuBar.add(helpMenu);
		setJMenuBar(menuBar);

		// setup events - point them all to this frame
		for (int i = 0; i < menuBar.getMenuCount(); ++i) {
			JMenu menu = menuBar.getMenu(i);
			for (int j = 0; j < menu.getItemCount(); ++j) {
				JMenuItem item = menu.getItem(j);
				if (item != null) {
					item.addActionListener(this);
				}
			}
		}

		// setup the controller
		manager = new IDEManager(this);

		// show window
		getContentPane().add(mainPanel);
		pack();
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		setVisible(true);
	}

	public void fileMenuEnabled(boolean value) {
		fileMenu.setEnabled(value);
	}

	@Override
	public void setTitle(String title) {
		super.setTitle(H20TITLE + ": " + title);
	}

	public void setEditorText(String value) {
		editorPanel.setText(value);
	}

	public void tabsEnabled(boolean value) {
		mainPanel.remove(editorPanel.asComponent());
		editorPanel.enableTabs(value);
		mainPanel.add(editorPanel.asComponent(), BorderLayout.CENTER);
	}

	public Programmable addTab(String name) {
		return editorPanel.add(name);
	}

	public Programmable addTab(String name, String text) {
		return editorPanel.add(name, text);
	}

	public void removeTab(String name) {
		editorPanel.remove(name);
	}

	public void showStatus(String text) {
		statusBar.setText(text);
	}

	// handle item events
	public void actionPerformed(ActionEvent e) {
		// figure out the menu and item for this event
		JMenuItem item = (JMenuItem) e.getSource();
		JPopupMenu p = (JPopupMenu) item.getParent();
		JMenu menu = (JMenu) p.getInvoker();
		String str = item.getText();

		// process the menu and item
		//String menuText = menu.getText();
		//System.out.println("" + item.getText() + " in: " + menuText);
		statusBar.setText("");
		if (menu == projectMenu) {
			if (str.equals(CREATE)) {
				manager.closeProject();
				manager.createProject();
				if (manager.projectOpen()) {
					runProject.setEnabled(true);
				}
			}
			if (str.equals(OPEN)) {
				manager.closeProject();
				manager.openProject();
				if (manager.projectOpen()) {
					runProject.setEnabled(true);
				}
			}
			if (str.equals(EXECUTE)) {
				manager.runProject();
			}
			if (str.equals(DELETE)) {
				manager.closeProject();
				manager.deleteProject();
				if (!manager.projectOpen()) {
					runProject.setEnabled(false);
				}
			}
		}
		if (menu == fileMenu) {
			if (str.equals(CREATE)) {
				manager.createFile();
			}
			if (str.equals(OPEN)) {
				manager.openFile();
			}
			if (str.equals(DELETE)) {
				manager.deleteFile();
			}
		}
		if (menu == helpMenu) {
			if (str.equals(ABOUT)) {
				JOptionPane.showMessageDialog(this, "H2OEditor - A Mobile Programming Editor\n" +
					"by Dr Jason Holdsworth\n" +
					"James Cook University, Cairns Campus\n" +
					"(c) 2010");
			}
			if (str.equals(H2OHELP)) {
				System.out.println("System\\runsilent \"C:\\Program Files\\Internet Explorer\\iexplore.exe\"" +
					" file:///" + System.getProperty("user.dir") + "\\doc\\index.html");
				Execute.runSeparate("System\\runsilent \"C:\\Program Files\\Internet Explorer\\iexplore.exe\"" +
					" file:///" + System.getProperty("user.dir") + "\\doc\\index.html");
			}
		}
	}

	// execution hook
	public static void main(String[] args) {
		MainWindow.create();
	}
}
